1. Field of the Invention
The invention relates to the field of object-oriented programming. More particularly, the invention relates to invocation on objects with interface inheritance.
2. Description of Related Art
Object-oriented programming is a method of software design implementation in which programs are organized as cooperative collections of objects. An object is a region of storage in a program and has a state, a behavior and an identity. Each object represents an instance of some class. A class is a set of objects that share a common structure and a common behavior. In an object-oriented programming language such as C++, a class is a user-defined type. A class declaration specifies the set of operations that can be applied to objects of the class.
An inheritance relationship unites a hierarchy of classes of a given program. A class can be derived from another class, and a class from which another class is derived is called a base class of the derived class. Further, a class may be derived from more than one class and inherits the operations and representation of each base class from which it is derived.
Given an object of some class, determining and choosing how to perform a dispatch (look-up) to the methods of the inherited classes is a problem. xe2x80x9cMethodxe2x80x9d is a term which comes from Smalltalk, an object-oriented programming language, and is equivalent to xe2x80x9cmember functionsxe2x80x9d in C++. xe2x80x9cMethodxe2x80x9d represents an operation declared as a member of a class.
In an interface inheritance, interfaces only inherit the definition of operations from other interfaces. In an implementation inheritance, interfaces inherit both the definition of the operation as well as the implementation of the operation from a base class definition. C++ uses implementation inheritance. Most C++ commercial products (compilers) use the concept of a vtable. A vtable is defined when an object is created, and has an entry for each method for each object requiring a dispatch to a possibly inherited implementation. A vtable typically consists of a list of pointers to methods. A Vtable typically also contains pointers to vtables for each of its base classes.
FIG. 1 illustrates one vtable implementation. The vtable implementation consists of BMW part 32 and its vtables 34, 35 and 36. BMW part 32 is derived from Vptr MW part 30, Vptr BW part 31 and Vptr W part 33. Vptr MW part 30 and BW part 31 are derived from Vptr W part 33. (See The Annotated C++ Reference Manual by Margaret A. Ellis and Bjarne Stroupstrup, Addison-Wesley Publishing Co., Massachusetts, p. 235 xc2xa710.10c.)
Disadvantages of using vtables are varied. Vtables for the base methods must be filled in to perform implementation inheritance. In addition, vtables for the base classes are specific for the derived class that the vtables are actually in and the base class that vtables are referring to. For each new derived class, a separate base vtable for each base class must be created. Thus, vtable implementation requires many vtables.
Further, the pointers to the vtables are typically mixed in with the data for the instance value variables of the classes themselves. In a typical vtable implementation, pointers point to the methods as well as the base object instances themselves which creates difficulty in moving the data structure from one address space to a different address space. Hence, the limitations of vtables include the lack of the ability to map object instances between address spaces and the number and complexity of the vtable entries.
The invention provides a method and apparatus for invocation of objects with interface inheritance. More specifically, the invention provides mtables as dispatch mechanism for object references that support only interface inheritance. An object reference using mtables contains two parts, more specifically, a pointer to the data for an object and a pointer to the methods on the object. The methods on the object are represented by a collection of mtables.
An mtable for a given interface consists of pointers to mtables for inherited interfaces and pointers to functions implementing the operations declared in the interface. An mtable pointer in an object reference points to an mtable for an apparent interface of the object reference. Mtables for any inherited interfaces are reached by indirection from the mtable for the apparent interface in the preferred embodiment.
Unlike vtables, the structure for an mtable is simple and may be implemented in essentially any programming language. Object references have a well known structure and hence may be passed from one language to another without modification. In addition, the data for an object is separated from the pointers to the methods on the object in an mtable. This separation allows potential mapping of data for an instance from one address space to another.
To invoke an operation on an mtable object reference requires one indirection from the object reference to the mtable for the interface, and one indirect call through a function pointer in the mtable. An additional indirection is used in the embodiment of the present invention to invoke methods from inherited interfaces. The additional indirection is from the mtable of the apparent type to the mtable for the type which declared the operation.
Widening is the process of creating a less derived object reference from a more derived object reference and is implemented by constructing a new object reference that consists of the original representation pointer and a pointer to the mtable for the wider interface. Narrowing, which is the process of creating a more derived object reference from a less derived object reference is implemented by constructing a new object reference that consists of the original representation pointer and a pointer to the mtable for the narrower interface.
Advantages of using mtables over prior art object reference implementations are that mtables support alternate implementations, mtables may be used from multiple languages, and mtables are small and fast. In addition, the method of the present invention allows mapping of data. Further, data does not have to be altered for widening or narrowing, and there is no possibility of copying only a widened view of the data when copying a widened object reference as an instance of the wider interface.